<?php

require './vendor/autoload.php';

//defaultLogge是一个快捷的方法；你也可以自己通过Monolog来创建自己的logger;
//$logger = \Elasticsearch\ClientBuilder::defaultLogger("./logs/test.log");
$logger = new \Monolog\Logger("test");
$logger->pushHandler(new \Monolog\Handler\StreamHandler("./logs/test.log", \Monolog\Logger::WARNING));
$client = \Elasticsearch\ClientBuilder::create()
    ->setHosts(array("192.168.166.35:9200"))
    ->setLogger($logger)
    ->build();

$csvFile = fopen("1.csv",'r');
while ($csvData = fgetcsv($csvFile)) { //每次读取CSV里面的一行内容
    if (!is_numeric($csvData[1])) {
        continue;
    }
    $params = array(
        'index' => 'fkl_ceshi',
        'type' => 'test_type',
        'id' => createUnique(),
        'body' => array(
            'id'=>(int) $csvData[0],
            'vcc_id'=>2,
            'call_id'=>$csvData[0],
            'caller' =>(string) $csvData[1],
            "start_time" => strtotime($csvData[5]),
            "que_id" => 2,
            "group_id" => 2,
            "all_secs" => 10,
            "server_num" => "57624342",
        )
    );
    $response = $client->index($params);
}

exit;
/**
 * 获取随机数
 * @return string
 */
function createUnique() {
    $data = time().rand();

    return md5($data);
    //return md5(time().$data);
}